<?php
  namespace app\redis\controller;
  class Friend extends \think\Controller{
       private $redis;
      function __construct(){
	    parent::__construct();
        $this->redis=new \redis();
	    $this->redis->connect('localhost',6379);
	    $this->redis->auth('1234');
       } 

	   function index(){//登入
	       return $this->fetch('index/login');
		}
   
      function friend_name(){//好友列表
		  $id=$this->redis->get('nameid');
		   for($v=1;$v<=$id;$v++){
		       $list[$v]=$this->redis->hgetall('name:'.$v);
			  
		   }
		   $list=array_filter($list);
          

		 

		  
		   if(\think\Cookie::get('nameid')){
			   $friend_list=[];
			   $fri=[];
			    $friend_name=$this->redis->smembers('friend:'.\think\Cookie::get('nameid'));
				foreach($friend_name as $v){
				   $friend_list[]=$this->redis->sdiff('friend:'.$v,'friend:'.\think\Cookie::get('nameid'));
				}
				 $friend_list=array_filter($friend_list);
				
				foreach($friend_list as $v){
				  foreach($v as $vv){
					if($vv!=\think\Cookie::get('nameid')){
					    $fri[]=$this->redis->hgetall('name:'.$vv);
					}
				    
				  }
				}
				$this->assign('friend',$fri);
			   $user=$this->redis->hgetall('name:'.\think\Cookie::get('nameid'));
		      echo "登入者:".$user['user'];
		   }


		  
		   $this->assign('list',$list);
	      return $this->fetch('index/friendname');
	  }
     

		function login(){
			$user=input('post.');
		   if(input('post.submit')){//注册
			   $nameid=$this->redis->incr('nameid');
			   $name=$this->redis->hmset('name:'.$nameid,['user'=>$user['user'],'pass'=>$user['pass']]);
			   if($name){
				 $this->redis->set($user['user'],$nameid);
			     echo '注册成功'."<a href='javascript:history.back();'>返回</a>|<a href='/redis.php/friend/friend_name'>好友列表</a>";
			   }else{
			     echo '注册失败'."<a href='javascript:history.back();'>返回</a>";
			  }
		   }else{//登录
		       $res=$this->redis->get($user['user']);
			   if($res){
				  $name=$this->redis->hgetall('name:'.$res);
			      if($user['pass']==$name['pass']){
					  \think\Cookie::set('nameid',$res);
				      echo '登录成功'."<a href='javascript:history.back();'>返回</a>|<a href='/redis.php/friend/friend_name'>好友列表</a>";
				  }else{
				     echo '登入失败'."<a href='javascript:history.back();'>返回</a>|<a href='/redis.php/friend/friend_name'>好友列表</a>";
				  }
			   }else{
			       echo '账号错误'."<a href='javascript:history.back();'>返回</a>|<a href='/redis.php/friend/friend_name'>好友列表</a>";
			   }
		  }
		}


   function friend_save(){
       $friend_id=intval(input('get.id'));
	   if($friend_id){
		   if(!$nameid=\think\Cookie::get('nameid')){
			   echo '你还未登入'."<a href='javascript:history.back();'>返回</a>|<a href='/redis.php/friend'>登入</a>";
		       die();
		   }

		   if($friend_id==$nameid){
		      echo '不能加自己为好友'."<a href='javascript:history.back();'>返回</a>";
		       die();
		   }
	        $res=$this->redis->sadd('friend:'.$nameid,$friend_id);
			if($res){
			    echo '添加成功'."<a href='javascript:history.back();'>返回</a>";
			}else{
			   echo '添加失败'."<a href='javascript:history.back();'>返回</a>";
			}
	   }
   }


   }
?>